/*
 * @Author: yushuang
 * @Date: 2024-12-03 15:58:14
 * @LastEditors: yushuang
 * @LastEditTime: 2025-02-10 10:28:22
 * @Description:
 */
import { createPinia } from 'pinia';
import { createApp } from 'vue';
import type { App } from 'vue';
import Root from './App.vue';
import router from '@/router';
import '@/router/router-guard';
import '@/assets/styles/index.less';
import 'ant-design-vue/dist/reset.css';
import { setupAccessDirective, setupLoadingDirective } from '@/directive';
// 配置qiankun
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
let app: any = null;

const pinia = createPinia();

async function start(props: any = {}) {
  const { container } = props;
  const app: App = createApp(Root);
  app.use(pinia);
  setupDirective(app);
  app.use(router);
  app.config.performance = true;
  app?.mount(container ? container.querySelector('#app') : '#app');
}

function setupDirective(app: App) {
  // 注册loading自定义指令
  setupLoadingDirective(app);
  setupAccessDirective(app);
}
renderWithQiankun({
  async mount(props: any) {
    await props.onGlobalStateChange((state: Record<string, any>) => {
      console.log('子应用监听state变化----', state);
      state.publicPath && window.localStorage.setItem('mainJumpPublicPath', state.publicPath);
    }, true);
    start(props);
  },
  bootstrap() {
    console.log('%c', 'color:green;', ' ChildOne bootstrap');
  },
  update() {
    console.log('%c', 'color:green;', ' ChildOne update');
  },
  unmount() {
    app.unmount();
    app._container.innerHTML = '';
    app = null;
  }
});
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  console.log('----并不是qiankun渲染---');
  start();
}
